import { formDate } from 'commonjs/formatDate.js'
export const dormDamageMixin = {
  data () {
    return {}
  },
  created () {
  },
  methods: {
    // 给表格添加索引
    indexMethod (index) {
      return (index + 1)
    },
    // 请求损坏列表
    async getDamagerUpList (tab, enven, tower = '桃园1') {
      // console.log(tab)
      this.damageUpList = []
      this.count = 0
      const Tower = tower
      // console.log(this.$router.currentRoute.path)
      const { data: res } = await this.$axios.get('/damage/findDamageUp', {
        params: {
          tower: this.$router.currentRoute.path === '/dormDamageUp' ? window.localStorage.getItem('tower') : tab ? tab.label : Tower,
          pagenum: tab ? 1 : this.dataList.pagenum,
          pagesize: this.dataList.pagesize
        }
      })
      // console.log(res)
      if (res.meta.code !== 200) {
        return this.$message('获取列表失败')
      } else {
        res.damageUpList.forEach(valid => {
          valid.time = formDate(valid.time)
          valid.finishTime = formDate(valid.finishTime)
          if (valid.finishTime === '1970-01-01 08:00:00') {
            valid.finishTime = ''
            this.damageUpList.push(valid)
          } else {
            this.damageUpList.push(valid)
          }
        })
        this.total = res.total
      }
      // console.log(this.damageUpList)
    },
    // 点击switch，获取表格当前行内容并显示到dialog
    // @change="finishFixedclick(scope.row._id,scope.row.isSolve,scope.row.tower)"
    async getSwitchCurRow (id, isSolve, tower, row) {
      if (isSolve) {
        const { data: res } = await this.$axios.get('/damage/serchFixMan',
          {
            params: { _id: id }
          })
        if (res.meta.code === 200) {
          this.costForm = res.result[0]
          this.dialogCostForm = true
          // 这里需要异步等待closeDialog方法执行完毕
          setTimeout(() => {
            if (this.switchMark) {
              row.isSolve = false
              this.switchMark = false
            }
          }, 2000)
        }
      } else {
        this.$confirm('此操作删除已上传的维修费用报表, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          this.finishFixedclick(id, isSolve, tower)
          const { data: res } = await this.$axios.get('/damage/removeCost', {
            params: {
              damageId: id
            }
          })
          if (res.meta.code !== -1) {
            this.$message.success(res.meta.msg)
          }
          this.dialogCostForm = false
          location.reload()
        }).catch(() => {
          // 让swich开关变回原来状态
          row.isSolve = !row.isSolve
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    // 上传维修费用
    async fixedCostUp () {
      console.log(this.num)
      const { data: res } = await this.$axios.get('/damage/cost', {
        params: {
          tower: this.costForm.tower,
          dorm: this.costForm.dorm,
          name: this.costForm.name,
          serviceman: this.costForm.serviceman,
          repalce: this.costForm.repalce,
          costs: this.costForm.costs,
          count: this.num,
          damageId: this.costForm._id
        }
      })
      // console.log(res)
      if (res.meta.code !== -1) {
        this.$message.success(res.meta.msg)
      }
    },
    // 点击dialog确定时触发的函数，修改已完成和未完成维修处理内容记录，这里主要用于dialog的表单验证
    finishFixed (id, isSolve, tower) {
      this.$refs.costForm.validate(async valid => {
        if (!valid) return 0
        this.finishFixedclick(id, isSolve, tower)
        // 防止还未上传维修数量数据，就导致维修数量数据重新赋值为0
        setTimeout(() => {
          this.num = 0
        }, 500)
      })
      // location.reload()
    },
    // 点击dialog取消时触发的函数
    closeDialog () {
      this.switchMark = true
      this.dialogCostForm = false
      // console.log(this.switchMark)
    }
  }
}
